import axios from 'axios';

function checkStatus(response) {
  if (response.status >= 200 && response.status < 300) {
    return response;
  }

  const error = new Error(response.statusText);
  error.response = response;
  throw error;
}

export function GET(url, headers) {
  if (headers) {
    return axios.get(url, {
      headers
    }).then(checkStatus)
    .then(data => ({ data }))
    .catch(err => ({ err }));
  } else {
    return axios.get(url)
    .then(checkStatus)
    .then(data => ({ data }))
    .catch(err => ({ err }));
  }

}

export function POST(url, params, headers) {
  return axios.post(url, params)
    .then(checkStatus)
    .then(data => ({ data }))
    .catch(err => ({ err }));
}
